#include "class_strategy_wave.h"
#include "class_center.h"
#include "class_strategy_position.h"

extern class_center a;
extern lambda_pool pool;


class_strategy_wave::class_strategy_wave(int idd, std::string dm, int n1, double bj, double zs, int sl)
{
memset(&ints,0,sizeof(ints));
memset(&dous,0,sizeof(dous));

memset(&data,0,sizeof(data));

name = dm;

std::string str = a.path_str + "/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/wave";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/wave/"+dm;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy/wave";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

ints[1] = n1;
ints[2] = sl;
dous[1]=bj;
dous[2]=zs;

cc = new class_strategy_position(a.path_str + "/strategy/wave/" + dm);

bars.read(a.path_str+"/bars/" + name + ".dat", n1+2);
if(a.bars_map.find(dm) != a.bars_map.end() )
{
for(auto &v: a.bars_map[dm].datas)
{
bars.datas.push_back(v);
}
}

//中轴·均线
dous[7] =bars.average(0, ints[1]);
dous[5] = dous[7] - dous[1] - dous[2];
dous[6] = dous[7] - dous[1];
dous[8] = dous[7] + dous[1];
dous[9]=dous[7]+dous[1] + dous[2];

id=idd;
//a.tick_event_map[name].insert([this](){run_one();},id);
//a.tick_event_map[name].insert([this](){ pool.insert([this](){run_one();}); },id);
a.tick_event_map[name].insert([this](){ pool.insert([this](){ if(a.flag[50]==1){run_one();} }); },id);
a.log.insert("strategy/wave/"+name,"实例化成功,当前仓位" + std::to_string(cc->position));
}

class_strategy_wave::~class_strategy_wave()
{
a.tick_event_map[name].pop(id);
	a.log.insert("strategy/wave/"+name,"析构完成,当前仓位"+std::to_string(cc->position));
delete cc;
}


// 主执行函数
void class_strategy_wave::run_one()
{


//清理报单返回值
a.return_erase(*cc);
//同步持仓
a.position_sync(*cc);
//获取到TICK
data=a.tick_map[name];

//获取行情到bar
bars.insert(data);
if (bars.datas.size() < (ints[1] + 2) || a.flag[35] == 0)
{
	std::cout<<"震荡"<<name<<"bar数量"<<bars.datas.size()<<std::endl;
return;
}




if (bars.datas.size() != ints[9])
{
ints[9] = bars.datas.size();
//中轴·均线
dous[7] =bars.average(0, ints[1]);
dous[5] = dous[7] - dous[1] - dous[2];
dous[6] = dous[7] - dous[1];
dous[8] = dous[7] + dous[1];
dous[9]=dous[7]+dous[1] + dous[2];

a.log.insert("strategy/wave/" + name, "中轴均线=" + std::to_string(dous[7]) + "多开" + std::to_string(dous[6]) + "多止损" + std::to_string(dous[5]) + "空开" + std::to_string(dous[8]) + "空止损" + std::to_string(dous[9]) + "持仓" + std::to_string(cc->position));
}

//如果处于买入区且仓位不够则
if (ints[5] < 1 &&data.AskPrice1 <= dous[6] && data.AskPrice1 > dous[5] && cc->position < ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '0', data.UpperLimitPrice, 1, *cc);
ints[5]=0;
a.log.insert("strategy/wave/" + name +"交易", name + "多开动作" + std::to_string(data.AskPrice1));
return;
}

if (ints[5] > -1 && data.BidPrice1 >= dous[8] && data.BidPrice1 < dous[9] && cc->position > 0 - ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '1', data.LowerLimitPrice, 1, *cc);
ints[5]=0;
a.log.insert("strategy/wave/" + name + "交易", name + "空开动作" + std::to_string(data.BidPrice1));
return;
}

if (data.BidPrice1 < dous[5] && cc->position > 0 && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '1', data.LowerLimitPrice, 1,*cc);
ints[5] = 1;
a.log.insert("strategy/wave/" + name + "交易", name + "多头止损" + std::to_string(data.BidPrice1));
return;
}

if (data.AskPrice1 > dous[9] &&  cc->position < 0 && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '0', data.UpperLimitPrice, 1, *cc);
ints[5] = -1;
a.log.insert("strategy/wave/" + name + "交易", name + "空头止损" + std::to_string(data.BidPrice1));
}

return;
}
